package com.example.jianyijisuanqi;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class chu extends AppCompatActivity implements View.OnClickListener{
    EditText et1;
    EditText et2;
    TextView tv;
    Button bt,fh,cx;
    int sum;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chu);
        et1 = findViewById(R.id.et1);
        et2 = findViewById(R.id.et2);
        tv = findViewById(R.id.tv);
        bt=findViewById(R.id.bt);
        bt.setOnClickListener(this);
        fh=findViewById(R.id.fh);
        fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(chu.this,MainActivity.class);
                startActivity(intent);
            }
        });

        cx=findViewById(R.id.cx);
        cx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /* 与数据库获得连接，获得只读属性 */
                SQLiteDatabase sqliteDatabase = helper.getReadableDatabase();
                Cursor cursor = sqliteDatabase.query(true,"history", new String[]{"first","second","third","forth","sum"}, null, null, null, null, null, null);
                /* 使用游标---获取游标中的数据 */
                while(cursor.moveToNext()){
                    @SuppressLint("Range") String first = cursor.getString(cursor.getColumnIndex("first"));
                    @SuppressLint("Range") String second = cursor.getString(cursor.getColumnIndex("second"));
                    @SuppressLint("Range") String third = cursor.getString(cursor.getColumnIndex("third"));
                    @SuppressLint("Range") String forth = cursor.getString(cursor.getColumnIndex("forth"));
                    @SuppressLint("Range") int sum = cursor.getInt(cursor.getColumnIndex("sum"));
                    Toast.makeText(chu.this, first+second+third+forth+sum, Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        String j1=et1.getText().toString();
        String j2=et2.getText().toString();
        String j3;
        int  sum1=Integer.valueOf(j1).intValue();
        int  sum2=Integer.valueOf(j2).intValue();
        sum=sum1/sum2;
        j3=String.valueOf(sum);
        tv.setText(j3);

        Intent intent = new Intent("com.example.jianyijisuanqi.broadcast1");
        intent.setComponent(new ComponentName("com.example.jianyijisuanqi","com.example.jianyijisuanqi.MyReceiver"));
        intent.putExtra("content",tv.getText().toString());
        sendBroadcast(intent,null);

        helper = new MySQLiteHelper(this);
        helper.getWritableDatabase();
        insert (j1,"/",j2,"=",sum);
    }

    MySQLiteHelper helper;
    public static final String TABLE_NAME = "history";

    public void insert (String first, String second, String third, String forth, int result) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put ("first", first);
        values.put ("second", second);
        values.put ("third", third);
        values.put ("forth", forth);
        values.put ("result", result);
        db.insert (TABLE_NAME, "id", values);
        db.close ();
    }
}
